package cn.lsc.org.service.impl;


import cn.lsc.basic.Exception.BusinessException;
import cn.lsc.basic.service.impl.BaseServiceImpl;

import cn.lsc.org.domain.Employee;
import cn.lsc.org.domain.Shop;
import cn.lsc.org.mapper.EmployeeMapper;
import cn.lsc.org.mapper.ShopMapper;
import cn.lsc.org.service.IEmployeeService;
import cn.lsc.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void settlement(Shop shop) {
        //实现shop入驻流程
        //1 非空校验 spring中提供了一个类 处理string
        if(StringUtils.isEmpty(shop.getName())
                ||StringUtils.isEmpty(shop.getAddress())
                ||StringUtils.isEmpty(shop.getTel())

                ||StringUtils.isEmpty(shop.getAdmin().getUsername())
                ||StringUtils.isEmpty(shop.getAdmin().getPhone())
                ||StringUtils.isEmpty(shop.getAdmin().getEmail())
                ||StringUtils.isEmpty(shop.getAdmin().getPassword())
                ||StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            throw  new BusinessException("参数不能为空");
        }
       Employee admin= shop.getAdmin();
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }
        //3校验该店铺是否被注册
        Shop dbshop=shopMapper.getByMessage(shop);
        if(dbshop!=null){
            throw new BusinessException("该店铺已经被注册");
        }
        //吧shop和employee数据保存在表中
        //
        employeeMapper.save(admin);
        //2吧自增主键设置到shop表中admin_id中
        admin.setShop_id(shop.getId());
        //3 保存shop数据，返回自增主键
        shopMapper.save(shop);
        //4 返回的自增主键给employee的shop_id
        admin.setShop_id(shop.getId());
        //5更新employee数据
        employeeMapper.update(admin);
    }
}
